package com.open.capacity.workflow.controller;


import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.workflow.model.WFDelegateRule;
import com.open.capacity.workflow.model.WFDelegateRuleRelation;
import com.open.capacity.workflow.service.WFDelegateRuleRelationService;
import com.open.capacity.workflow.service.WFDelegateRuleService;
import com.open.capacity.workflow.vo.WFDelegateRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 作者 ftquan 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * 流程委托
 */
@RestController
@Api(tags = "流程委托模块api")
public class WFDeleagateController {



    @Autowired
    private WFDelegateRuleService delegateRuleService;

    @Autowired
    private WFDelegateRuleRelationService delegateRuleRelationService;


    /**
     * 查询流程委托规则
     *
     * @param params
     * @return
     */
    @PreAuthorize("hasAuthority('wfdelegate:get/wfdelegates')")
    @ApiOperation(value = "查询流程委托规则")
    @GetMapping("/wfdelegates")
    @LogAnnotation(name = "流程",module = "workflow-center", recordRequestParam = false)
    public PageResult<WFDelegateRule> findDelegateRules(@RequestParam Map<String, Object> params) {
        return delegateRuleService.pageDelegateRules(params);
    }


    /**
     * 查询流程委托关联
     *
     * @param params
     * @return
     */
    @PreAuthorize("hasAuthority('wfdelegate:get/wfdelegates')")
    @ApiOperation(value = "查询流程委托关联")
    @GetMapping("/wfdelegates/getRelationList")
    @LogAnnotation(name = "流程",module = "workflow-center", recordRequestParam = false)
    public List<WFDelegateRuleRelation> getRelationList(@RequestParam Map<String, Object> params) {
        return delegateRuleRelationService.findDelegateRuleRelations(params);
    }

//    @ApiOperation(value = "获取流程委托")
//    @PreAuthorize("hasAuthority('wfdelegate:get/wfdelegates/{id}')")
//    @GetMapping("/wfdelegates/{id}")
//    public Result<SchemeVo> findSchemeById(@PathVariable(value = "id") String id) {
//        SchemeVo schemeinfo = delegateRuleService.findById(id);
//        List<WFSchemeAuthorize> authorizes = delegateRuleService.findBySchemeinfoId(id);
//        schemeinfo.setShcemeAuthorizes(authorizes);
//        return Result.succeed(schemeinfo);
//    }


    /**
     * 流程委托新增或者更新
     *
     * @param delegateRule
     * @param keyValue
     * @return
     */
    @PreAuthorize("hasAnyAuthority('wfdelegate:post/wfdelegates','wfdelegate:put/wfdelegates')")
    @ApiOperation(value = "保存流程委托")
    @PostMapping("/wfdelegates/saveOrUpdate")
    @LogAnnotation(name = "流程",module = "workflow-center", recordRequestParam = false)
    public Result saveOrUpdate(@RequestBody WFDelegateRuleVo delegateRule, String keyValue) {
        return delegateRuleService.saveOrUpdate(delegateRule, keyValue);
    }


    /**
     * 删除
     * delete /wfdelegates/1
     *
     * @param id
     */
    @PreAuthorize("hasAuthority('wfdelegate:delete/wfdelegates/{id}')")
    @ApiOperation(value = "删除流程委托")
    @DeleteMapping("/wfdelegates/{id}")
    @LogAnnotation(name = "流程",module = "workflow-center", recordRequestParam = false)
    public Result delete(@PathVariable(value = "id") String id) {
        try {
            delegateRuleService.delete(id);
            return Result.succeed("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("操作失败");
        }
    }

}
